﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Hotnet.Biz.NSBizCommon;
using Hotnet.Entity.Enum;
using Hotnet.Biz.NSBizPicture;
using Hotnet.Web.Filters;
using Hotnet.Web.Utility;
using Hotnet.Entity;
using Hotnet.Biz.NSBizVideo;
using Hotnet.Biz.NSBizNews;

namespace Hotnet.Web.Controllers.Front
{
	[FrontFilter(EnumSystemCategory.图片)]
	public class PictureController : FrontBaseController
	{

		//
		// GET: /PictureHome/
		public ActionResult Index()
		{
			ViewData["HeadControl"] = new HeadControl() { SystemCategory = EnumSystemCategory.图片};
			//ViewData["BigEyes"] = BizBigEye.ListTop(EnumSystemCategory.图片, 4);
			ViewData["BigEyes"] = BizPictureList.List(EnumPictureListType.首页焦点图片, 4);
			ViewData["PictureCategories"] = BizPictureGroup.ListTopXPictureGroupByCategory(6);
			ViewData["HotPictureGroups"] = BizPictureGroup.ListHotGroups(13);
			ViewData["HotPictureTag"] = new TagControl(){ Tags= BizTag.ListClickHotTag(40, EnumTagType.图片), IsOpenNewPage=true, Url="/PictureTagSearch"};
			//列出一周的最新图片
			ViewData["PictureGroupNewest"]=BizPictureGroup.ListNewestPictureGroup(7,10);
			//ViewData["CommendPictureGroups"] = BizPictureGroup.ListRecommendPictureGroup(6);
			ViewData["CommendPictureGroups"] = BizPictureList.List( EnumPictureListType.推荐图片,6);
			return View();
		}

		public ActionResult Category(int id, int? pageNo)
		{
			int totalCount;
			var groups = BizPictureGroup.List(id, pageNo ?? 0, 24, out totalCount);
			PictureCategory category = BizPictureCategory.Get(id);
			ViewData["PaginateHelper"] = PaginateHelper.ConstructPaginate(totalCount, pageNo ?? 0, 24, string.Format("/Picture/Category/{0}/", id), null);
			ViewData["Category"] = category;
			ViewData["HotPictureGroups"] = BizPictureGroup.ListHotGroups(id, 10);
			//ViewData["CommendPictureGroups"] = BizPictureGroup.ListRecommendPictureGroup(id, 6);
			ViewData["CommendPictureGroups"] = BizPictureList.List(EnumPictureListType.推荐图片, id, 6);
			ViewData["RefVideoControl"] = new ReferenceVideoControl() { ReferenceVideos = BizVideo.ListReferenceVideoByTags(category.Tag, 12, true), VideoListStyle = EnumVideoListStyle.horizontal, ModuleTitle="您可能感兴趣的视频" };
			ViewData["PictureRotator"] = new PictureRotatorControl() { Groups = BizPictureList.List( EnumPictureListType.分类焦点图片,id, 6) };
			ViewData["RefNews"] = BizNews.ListReferenceNewsByTags(category.Tag, 25, true);
			ViewData["IsAnchor"] = pageNo != null ? "True" : "False";
			ViewData["HeadControl"] = new HeadControl() { SystemCategory = EnumSystemCategory.图片, SystemCategoryID = id };
			return View(groups);
		}

		//[FrontFilter(EnumSystemCategory.图片, NeedLoadRelational = true)]


		/// <summary>
		/// 
		/// </summary>
		/// <param name="id">PictureGroup id</param>
		/// <param name="cId">图片组中 当前显示的图片id</param>
		/// <returns></returns>
		public ActionResult Group(int? id,int? cId)
		{
			if (id == null)
				return null;
			int preId = -1;
			int nextId = -1;
			//获得PictureGroup信息,并得到SourceFrom
			PictureGroup group= BizPictureGroup.GetPictureGroupIncludeSourceFrom(id.Value);
			//列出此组的所有图片,需要缓存
			Picture[] pictures = BizPicture.List(id.Value);
			//列出相关图片组
			ViewData["RelationalGroups"] = BizPictureGroup.ListReferenceGroupsByGroupID(id.Value, 10);
			//获得图片的Tag
			List<string> tagNames = BizPictureGroupXTag.ListTagNamesByGroupID(id.Value);
			//获得相关的视频
			ViewData["RefVideoControl"] = new ReferenceVideoControl() { ReferenceVideos = BizVideo.ListReferenceVideoByTags(string.Join(" ", tagNames.ToArray()), 10, true), VideoListStyle = EnumVideoListStyle.horizontal, ModuleTitle = "您可能感兴趣的视频", MoreLink = "/VideoRecommandMore" };
			//获得相关的新闻
			ViewData["RefNews"] = BizNews.ListReferenceNewsByTags(tagNames,20,true);
			//header
			ViewData["HeadControl"] = new HeadControl() { SystemCategory = EnumSystemCategory.图片, SystemCategoryID = group.CategoryID };
			Picture currentPicture = null;
			//处理上一张与下一张图片
			int[] pictureIds = pictures.Select(x => x.PictureID).ToArray();
			int picturePosition = 0;
			if (cId == null && pictures != null && pictures.Length > 0)
			{
				cId = pictures[0].PictureID;
				currentPicture = pictures[0];
			}
			else
			{
				currentPicture = pictures.Where(x => x.PictureID == cId).First();
			}

			DealPreNext(pictureIds, cId ?? -1, ref preId, ref nextId, ref picturePosition);

			ViewData["PictureGroupLength"] = pictureIds.Length;
			ViewData["PicturePosition"] = picturePosition;
			ViewData["PrePictureID"] = preId;
			ViewData["NextPictureID"] = nextId;
			ViewData["PictureGroup"] = group;
			ViewData["PictureCurrent"] = currentPicture;
			ViewData["PictureTags"] = BizPictureGroupXTag.ListTagNamesByGroupID(id.Value);
			
			//点击数+1
			BizPictureGroup.IncreaseClickCount(id.Value, group.ClickCount ?? 0);
			return View();
		}

		/// <summary>
		/// 返回prePictureId和nextPicture
		/// </summary>
		/// <param name="data"></param>
		/// <param name="current"></param>
		/// <param name="pre"></param>
		/// <param name="next"></param>
		private static void DealPreNext(int[] data, int current, ref int pre, ref int next,ref int picturePosition)
		{
			if (data == null || data.Length <= 1)
			{
				pre = -1;
				next = -1;
			}
			for (int i = 0; i < data.Length; i++)
			{
				if (data[i] == current)
				{
					picturePosition = i+1;
					if (i != 0)
						pre = data[i - 1];
					if (i != data.Length - 1)
						next = data[i + 1];
					return;
				}
			}
		}

		/// <summary>
		/// 混淆图片id算法
		/// </summary>
		/// <param name="id"></param>
		private static int ConfusePictureID(int id)
		{
			return 0;
		}


		//private BizBigEye bizBigEye;
		//private BizBigEye BizBigEye
		//{
		//    get
		//    {
		//        if (bizBigEye == null)
		//            bizBigEye = new BizBigEye();
		//        return bizBigEye;
		//    }
		//}

		private BizPictureList bizPictureList;
		private BizPictureList BizPictureList
		{
			get
			{
				if (bizPictureList == null)
					bizPictureList = new BizPictureList();
				return bizPictureList;
			}
		}

		private BizPicture bizPicture;
		private BizPicture BizPicture
		{
			get
			{
				if (bizPicture == null)
					bizPicture = new BizPicture();
				return bizPicture;
			}
		}

		private BizPictureCategory bizPictureCategory;
		private BizPictureCategory BizPictureCategory
		{
			get
			{
				if (bizPictureCategory == null)
					bizPictureCategory = new BizPictureCategory();
				return bizPictureCategory;
			}
		}

		private BizPictureGroup bizPictureGroup;
		private BizPictureGroup BizPictureGroup
		{
			get
			{
				if (bizPictureGroup == null)
					bizPictureGroup = new BizPictureGroup();
				return bizPictureGroup;
			}
		}

		private BizPictureGroupXTag bizPictureGroupXTag;
		private BizPictureGroupXTag BizPictureGroupXTag
		{
			get
			{
				if (bizPictureGroupXTag == null)
					bizPictureGroupXTag = new BizPictureGroupXTag();
				return bizPictureGroupXTag;
			}
		}

		private BizVideo bizVideo;
		private BizVideo BizVideo
		{
			get
			{
				if (bizVideo == null)
					bizVideo = new BizVideo();
				return bizVideo;
			}
		}

		private BizNews bizNews;
		private BizNews BizNews
		{
			get
			{
				if (bizNews == null)
					bizNews = new BizNews();
				return bizNews;
			}
		}

		private BizTag bizTag;
		private BizTag BizTag
		{
			get
			{
				if (bizTag == null)
					bizTag = new BizTag();
				return bizTag;
			}
		}

	}
}
